package com.spzx.channel.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.spzx.channel.domain.ItemVo;
import com.spzx.channel.service.IItemService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.domain.*;
import io.swagger.v3.oas.annotations.media.Schema;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.HashAttributeSet;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class IItemServiceImpl implements IItemService {

    //注入：定义远程调用各个接口
    @Autowired
    private RemoteProductService remoteProductService;

    @Override
    public ItemVo getProductSkuById(Long skuId) {
        ItemVo itemVo = new ItemVo();

//        远程调用ProductController里的方法获取到ProductSku的信息 也是ItemVo的第一个属性 其他的属性也是远程调用获取
        R<ProductSku> r1 = remoteProductService.getProductSkuById(skuId, SecurityConstants.INNER);
        ProductSku productSku = r1.getData();

//        商品信息
        R<Product> r2 = remoteProductService.getProductByProductId(productSku.getProductId(), SecurityConstants.INNER);
        Product product = r2.getData();

//        最新价格信息
        R<SkuPrice> r3 = remoteProductService.getSkuPriceBySkuId(skuId, SecurityConstants.INNER);
        SkuPrice skuPrice = r3.getData();

//        商品轮播图列表   Product对象的构造方法获取到urls  转化为List<String> urlList 赋值给ItemVo
        List<String> urlList = null;
        String sliderUrls = product.getSliderUrls();
        if (sliderUrls.length() != 0){
            String[] urls = sliderUrls.split(",");
            urlList = Arrays.asList(urls);
        }

//        商品详细图片列表
        List<String> detailsUrlList = null;
        R<ProductDetails> r4 = remoteProductService.getProductDetailsByProductId(product.getId(), SecurityConstants.INNER);
        ProductDetails productDetails = r4.getData();
        if (productDetails == null){
            System.out.println("查询图片列表失败");
        }
        String imageUrls = productDetails.getImageUrls();
        if (imageUrls.length() != 0 ){
            String[] urls = imageUrls.split(",");
            detailsUrlList = Arrays.asList(urls);
        }
//        商品规格信息  spu 中有specValue属性 获取到String后转为JSONArray类型
        String specValue = product.getSpecValue();
        JSONArray specValueJson = JSONArray.parseArray(specValue);

//        商品库存信息
        R<SkuStockVo> r5 = remoteProductService.getSkuStockVoBySkuId(skuId, SecurityConstants.INNER);
        SkuStockVo skuStock = r5.getData();

//        商品规格对应商品skuId信息
        R<Map<String, Long>> r6 = remoteProductService.getSkuSpecValueMap(product.getId(), SecurityConstants.INNER);
        Map<String, Long> skuSpecValueMap = r6.getData();


        itemVo.setProductSku(productSku);
        itemVo.setProduct(product);
        itemVo.setSkuPrice(skuPrice);
        itemVo.setSliderUrlList(urlList);
        itemVo.setSliderUrlList(detailsUrlList);
        itemVo.setSpecValueList(specValueJson);
        itemVo.setSkuStockVo(skuStock);
        itemVo.setSkuSpecValueMap(skuSpecValueMap);
        return itemVo;
    }
}
